﻿require(["avalon", "jquery"], function () {
	var vm = avalon.define({
		$id: "login",
		mobile: "",
		code: "",
		agentId: 0,
		openId: "",
		nick: "",
        headimgurl:"",
        register: function () {
        	if (vm.mobile.length <= 0)
        	{
        		show_msg("请输入手机号");
        		return;
        	}
			if(!isMobile(vm.mobile))
			{
				show_msg("手机号码格式不正确");
				return;
			}
			if(vm.code.length<=0)
			{
				show_msg("请输入短信验证码");
				return;
			}
			vm.agentId = $("#agentId").val();
			vm.openId = $("#openId").val();
			vm.nick = $("#nick").val();
			vm.headimgurl = $("#headimgurl").val();
			show_waitting();
			$.post('register',
                    { mobile: vm.mobile, code: vm.code, agentId: parseInt(vm.agentId), openId: vm.openId, nickName: vm.nick, headimgurl: vm.headimgurl },
                    function (data) {
                        if (data.success) {
                            close_waitting();
                            show_msg("注册成功");
                            location.href="/wechat/member/index"
                        }
                        else {
                            alert(data.msg);
                        }
                    });
        },
        getSmsCode:function(){
        	if (vm.mobile.length <= 0) {
        		show_msg("请输入手机号");
        		return;
        	}
        	if (!isMobile(vm.mobile)) {
        		show_msg("手机号码格式不正确");
        		return;
        	}
        	else {
        	    //请求发送短信验证码
        	    show_waitting();
        	    $.post('SendRegisterSmsCode',
                    { mobile: vm.mobile },
                    function (data) {
                        if (data.success) {
                            close_waitting();
                            settime();
                        }
                        else {
                            alert(data.msg);
                        }
                    });
        	}
        }

	});
});

function isMobile(mobile) {
	if (!/^(((13[0-9]{1})|(15[0-9]{1})|(17[0-9]{1})|(14[0-9]{1})|(18[0-9]{1}))+\d{8})$/.test(mobile))
		return false;
	return true;
}

var countdown = 60;
function settime() {
    val=$("#btnSendSms")
	if (countdown == 0) {
		val.removeAttr("disabled");
		$(val).val("重新发送");
		countdown = 60;
	} else {
		val.attr("disabled", "disabled");
		$(val).val("重新发送(" + countdown + ")");
		countdown--;
		setTimeout(function () {
			settime(val)
		}, 1000)
	}
}